﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase.Form.Control;
using Microsoft.Xna.Framework;
using GameBase;

namespace GalaxyPresident
{
    class MainMenuScreen : GameScreen
    {
        public MainMenuScreen(Game1 game)
            : base(game)
        {
            InitForm();
            Perspective3DGroup layer = new Perspective3DGroup(Game.GraphicsDevice.Viewport, 250);
            
            for (int i = 0; i < 20; i++)
            {
                int b = 500;
                int a = 1000;
                int x = Constant.random.Next(2 * a) - a;
                int y = Constant.random.Next(b * 2) - b;

                Visble3DObject star = new Visble3DObject(@"Resources/star", 1, 3,
                    new Vector3(x, y, Constant.random.Next(400) - 500));
                star.Sprite.FrameSequence = new List<int> { 0, 1, 2, 1 };
                star.Sprite.CurrentFrame = Constant.random.Next(4);
                star.Sprite.Delay = Constant.random.Next(50) + 50;
                layer.AddChild(star);
            }
            for (int i = 0; i < 1; i++)
            {
                int b = 500;
                int a = 1000;
                int x = Constant.random.Next(2 * a) - a;

                int y = Constant.random.Next(b * 2) - b;

                Visble3DObject star = new Visble3DObject(@"Resources/nebula", 1, 1,
                    new Vector3(x, y, Constant.random.Next(600) - 500));
                star.Scale = (float)(Constant.random.NextDouble() * 2 + 1);
                star.Rotate = (float)(Constant.random.NextDouble() * MathHelper.TwoPi);
                star.Sprite.SetOriginPosition(ORIGIN_POSITION.Center);
                layer.AddChild(star);
            }
            AddChild(layer);
        }
        MyPanel panel;
        private GameScreen nextScreen;
        private bool isOut = false;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (!isOut && panel.Position.Y < 100)
            {
                panel.Translate(0,50);
            }
            else if (isOut)
            {
                if (panel.Position.Y > -500)
                {
                    panel.Translate(0, -50);
                }
                else
                {
                    if (nextScreen == null)
                        Game.Exit();
                    Game.State = nextScreen;
                }
            }
        }
        public void ChangeScreen(GameScreen screen)
        {
            nextScreen = screen;
            isOut = true;
        }
        // form init
        private void InitForm()
        {
            panel = new MyPanel(@"Resources/menupanel",
                new Vector2((Form.Width-400)/2, -500));
            panel.Color = Color.Cyan;
            AddControl(panel);

           

            MyButton buttonSinglePlay = new MyButton(new Vector2(150, 100), "SinglePlay");
            buttonSinglePlay.SetRandomColor();
            buttonSinglePlay.MouseReleasedCallBack = buttonSinglePlayReleased;
            panel.Add(buttonSinglePlay);

            MyButton buttonMultiPlay = new MyButton(new Vector2(150, 150), "MultiPlay");
            buttonMultiPlay.SetRandomColor();
            buttonMultiPlay.MouseReleasedCallBack = buttonMultiPlayReleased;
            panel.Add(buttonMultiPlay);

            MyButton buttonOption = new MyButton(new Vector2(150, 200), "Option");
            buttonOption.SetRandomColor();
            buttonOption.MouseReleasedCallBack = buttonOptionReleased;
            panel.Add(buttonOption);

            MyButton buttonAbout = new MyButton(new Vector2(150, 250), "About");
            buttonAbout.SetRandomColor();
            buttonAbout.MouseReleasedCallBack = buttonAboutReleased;
            panel.Add(buttonAbout);


            MyButton buttonExit = new MyButton(new Vector2(150, 500), "Exit");
            buttonExit.SetRandomColor();
            buttonExit.MouseReleasedCallBack = buttonExitReleased;
            //buttonExit.Depth = 0;
            panel.Add(buttonExit);

        }
        private void buttonSinglePlayReleased(MyControl sender)
        {
            ChangeScreen( new SinglePlayScreen(Game));
        }
        private void buttonMultiPlayReleased(MyControl sender)
        {
            /*PlayingScreen screen = new PlayingScreen(Game);
            screen.CurrentLayer = 2;*/
            ChangeScreen(new MutiPlayScreen(Game));
        }
        private void buttonOptionReleased(MyControl sender)
        {
            ChangeScreen(new OptionScreen(Game));
        }
        private void buttonAboutReleased(MyControl sender)
        {
            ChangeScreen(new AboutScreen(Game));
        }
        private void buttonExitReleased(MyControl sender)
        {
            ChangeScreen(null);
        }
    }
}
